const mongoose = require('mongoose');

// 定义Comment Schema
const commentSchema = new mongoose.Schema({
  _id: {
    type: Number,
    required: true
  },
  content: {
    type: String,
    required: [true, '评论内容是必需的']
  },
  author: {
    type: String,
    required: [true, '评论作者是必需的'],
    trim: true
  },
  postId: {
    type: Number,
    required: [true, '文章ID是必需的']
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

// 静态验证方法
commentSchema.statics.validateComment = function (commentData) {
  const errors = [];
  
  if (!commentData.content || commentData.content.trim() === '') {
    errors.push('评论内容是必需的');
  }
  
  if (!commentData.author || commentData.author.trim() === '') {
    errors.push('评论作者是必需的');
  }
  
  if (!commentData.postId) {
    errors.push('文章ID是必需的');
  }
  
  return {
    valid: errors.length === 0,
    errors
  };
};

// 创建Comment模型
const Comment = mongoose.model('Comment', commentSchema);

// 初始评论数据
const comments = [
  { id: 1, content: '这是第一条评论，写得非常好！', author: '读者甲', postId: 1, createdAt: new Date() },
  { id: 2, content: '感谢分享这些技巧，很有用。', author: '读者乙', postId: 2, createdAt: new Date() },
  { id: 3, content: '期待更多关于Vue的文章。', author: '读者丙', postId: 3, createdAt: new Date() }
];

module.exports = {
  Comment,
  comments
};